package com.example.user.activityandintent;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
    RadioButton r1 = null;
    RadioButton r2 = null;
    RadioButton r3 = null;
    RadioButton r4 = null;
    RadioGroup radioGroup = null;
    //RadioButton currentRadioButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取单选按钮组
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        //获取单选按钮
        r1 = (RadioButton) findViewById(R.id.a);
        r2 = (RadioButton) findViewById(R.id.b);
        r3 = (RadioButton) findViewById(R.id.c);
        r4 = (RadioButton) findViewById(R.id.d);
        r1.setClickable(true);
        //监听单选按钮
        //radioGroup.setOnCheckedChangeListener();
        Button btn1_sure = (Button) findViewById(R.id.sure);
        Button btn2_cannel = (Button) findViewById(R.id.cancel);
        btn1_sure.setOnClickListener(new btn1_sure());
        btn2_cannel.setOnClickListener(new btn2_cannel());
    }

    /*
    * class btn1_sure implements View.OnClickListener {
        public void onClick(View v) {
            if (currentRadioButton.getText().equals("这个？")) {
                setTitle("冰菓");
            } else if (currentRadioButton.getText().equals("也许是这个")) {
                setTitle("again");
            } else if (currentRadioButton.getText().equals("确定正确率是25%的选项")) {
                setTitle("不是");
            } else if (currentRadioButton.getText().equals("100%确定错误率是25%的选项")) {
                setTitle("No");
            }
    * */

    class btn1_sure implements View.OnClickListener {
        public void onClick(View v) {
            String ans = "";
            if (r1.isChecked()) {
                ans = "这个？";
            } else if (r2.isChecked()) {
                ans = "也许是这个";
            } else if (r3.isChecked()) {
                ans = "确定正确率是25%的选项";
            } else if (r4.isChecked()) {
                ans = "100%确定错误率是25%的选项";
            }

            Intent intent = new Intent();
            //设置intent要启动的Activity
            intent.setClass(MainActivity.this,OtherActivity.class);
            //数据传输
            Bundle bundle = new Bundle();
            bundle.putString("ans", ans);
            intent.putExtras(bundle);
            //通过intent启动另一个Activity
            //MainActivity.this.startActivity(intent);

            startActivityForResult(intent,0);
        }
    }

    class btn2_cannel implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            radioGroup.clearCheck();
            setTitle("");
        }

    }


      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (resultCode) {
                case RESULT_OK:
            //取得来自Activity2 的数据，并显示于画面上
                    Bundle bundle = data.getExtras();
                    String ans = bundle.getString("ans");

                    break;
                default:
                    break;
            }
        }
}
